/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */
import _ from 'lodash';
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {

    componentDidMount() {

        fetch("http://localhost:3000", {
            method: "POST",
            headers: {
                "Accept": "application/json",
                'content-type': 'application/x-www-form-urlencoded'
            },
            body: this._paramPost({
                select: {
                    user: {
                        table: 'user',
                        // column: 'id, name, nick, image',
                        where: {
                            id: 1
                        }
                    },
                    user2: {
                        table: 'user',
                        column: 'id, name, nick, image',
                        where: {
                            id: 1
                        }
                    }
                },
                insert: {
                    product: {
                        name: '',
                        mark: '',
                        price: ''
                    }
                },
                update: {
                    product: {
                        column: {
                            name: '',
                            price: ''
                        },
                        where: {
                            id: 3
                        }
                    }
                },
                delete: {
            
                }
            })
        }).then(res => {
            return res.text();
        }).then(text => {
            console.log('return')
            console.log(text);
        })
    }

    /**
     * 设置Post方法的参数
     * 
     * @param {Object} data 数据对象
     * @return {FormData} 如果存在data，则返回新的formData数据，否则返回null
     */
    _paramPost(data) {

        // 如果不是平面对象，则直接返回false（代表不需要在obj添加body）
        if (!_.isPlainObject(data)) {
            return null;
        }

        // 制作formData对象，并返回
        let formData = new FormData();
        formData.append('body', JSON.stringify(data));
        // for (let key in data) {
        //     formData.append(key, data[key])
        // }

        // 返回formData数据（代表需要在obj添加body）
        return formData;
    }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});
